package com.controller.cook;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.po.Dish;
import com.po.MyMessage;
import com.po.TodaymenuY;
import com.po.TomorrowmenuY;
import com.service.cook.CcommentServiceY;
import com.service.cook.CdishServiceY;
import com.service.cook.CmessageServiceY;
import com.service.cook.CtodaymenuServiceY;
import com.service.cook.CtomorrowmenuServiceY;
import com.util.Yutil;

@Controller
public class CControllerY {
	@Resource(name = "cdishServiceImpY")
	private CdishServiceY cdishService;
	@Resource(name = "ctodaymenuServiceImpY")
	private CtodaymenuServiceY ctodaymenuService;
	@Resource(name = "ctomorrowmenuServiceImpY")
	private CtomorrowmenuServiceY ctomorrowmenuService;
	@Resource(name = "ccommentServiceImpY")
	private CcommentServiceY ccommentService;
	@Resource(name = "cmessageServiceImpY")
	private CmessageServiceY cmessageService;
	@ResponseBody
	@RequestMapping(value = "/delMessageY")
	public int delMessage(@RequestParam("message_id") String message_id) {
		return cmessageService.delMessage(message_id);
	}
	@ResponseBody
	@RequestMapping(value = "/addMessageY")
	public int addMessage(@RequestBody MyMessage message) {
		return cmessageService.addMessage(message);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getNumberByParentY")
	public int getNumberByParent(@RequestParam("message_parentid") String message_parentid) {
		return cmessageService.getNumberByParent(message_parentid);	
	}
	
	@ResponseBody
	@RequestMapping(value = "/getAllMessageByIDY")
	public List<MyMessage> getAllMessageByID(@RequestParam("userid") String userid) {
		List<MyMessage> list = cmessageService.getMessageByID(userid);
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/getAllMessageY")
	public List<MyMessage> getAllMessage() {
		List<MyMessage> list = cmessageService.getAllMessage();
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/getAllMessageByParentY")
	public List<MyMessage> getAllMessageByParent(@RequestParam("message_parentid") String message_parentid) {
		List<MyMessage> list = cmessageService.getAllMessageByParent(message_parentid);
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/updataImageY")
	public int updataImage(@RequestBody Dish dish, HttpServletRequest request) {
		System.out.println(dish.getImage());
		//判断是否上传了图片
		if(dish.getImage()!=null&&!dish.getImage().equals("")){
			byte[] imagebyte = dish.getImage();
			int j = Yutil.byte2image(imagebyte,request.getServletContext().getRealPath("/images/cook/") + dish.getDish_imagepath());
			if(j==1){
				return cdishService.updateDish(dish);
			}
			else return 0;
		}
		else{
			return cdishService.updateDish(dish);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/addImageY")
	public int addImage(@RequestBody Dish dish, HttpServletRequest request) {
		//System.out.println(image.getImage()[0]);
		//判断是否上传了图片
		if(dish.getImage()!=null&&!dish.getImage().equals("")){
			byte[] imagebyte = dish.getImage();
			int j = Yutil.byte2image(imagebyte,request.getServletContext().getRealPath("/images/cook/") + dish.getDish_imagepath());
			if(j==1){
				return cdishService.addDish(dish);
			}
			else return 0;
		}
		else{
			return cdishService.addDish(dish);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/tomorrowtoTodayY")
	public int tomorrowtoToday() {
		List<TomorrowmenuY> tomorrow = ctomorrowmenuService.getTomorrowmenuList();
		List<TomorrowmenuY> today = new ArrayList<TomorrowmenuY>();
		if (tomorrow.size() > 6) {
			for (int j = 0; j < 6; j++) {
				today.add(tomorrow.get(j));
			}
			ctodaymenuService.addTodayMenus(today);
			ccommentService.addComment(today);
			return 1;
		}
		return 0;
	}

	@ResponseBody
	@RequestMapping(value = "/havetodayY")
	public int havetoday() {
		int i = ctodaymenuService.havetoday();
		return i;
	}

	@ResponseBody
	@RequestMapping(value = "/havetomorrowY")
	public int havetomorrow() {
		int i = ctomorrowmenuService.haveTomorrow();
		return i;
	}

	@ResponseBody
	@RequestMapping(value = "/addTomorrowMenusY")
	public int addTomorrowMenus(@RequestBody List<Dish> list) {
		int i = ctomorrowmenuService.addTomorrowmenus(list);

		return i;
	}

	@ResponseBody
	@RequestMapping(value = "/getTomorrowMenuListY")
	public List<TomorrowmenuY> getTomorrowMenuList() {
		List<TomorrowmenuY> list = ctomorrowmenuService.getTomorrowmenuList();
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/delTomorrowMenuY")
	public int delTomorrowMenu() {
		int i = ctomorrowmenuService.delTomorrowmenu();
		return i;
	}

	@ResponseBody
	@RequestMapping(value = "/delTodayMenuY")
	public int delTodayMenu() {
		int i = ctodaymenuService.delTodayMenu();
		return i;
	}

	@ResponseBody
	@RequestMapping(value = "/addTodayMenusY")
	public int addTodayMenu(@RequestBody List<TomorrowmenuY> list) {
		int i = ctodaymenuService.addTodayMenus(list);
		return i;
	}

	@ResponseBody
	@RequestMapping(value = "/getTodayMenuListY")
	public List<TodaymenuY> getTodayMenuList() {
		List<TodaymenuY> list = ctodaymenuService.getTodayMenuList();
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/finddishY")
	public Dish findDish(@RequestParam("dish_id") String dish_id) {
		Dish dish = cdishService.findDishOne(dish_id);
		return dish;
	}

	@ResponseBody
	@RequestMapping(value = "/getdishbynameY")
	public List<Dish> getDishByName(@RequestParam("dish_name") String dish_name) {
		List<Dish> dish = cdishService.findDishByName(dish_name);
		return dish;
	}

	@ResponseBody
	@RequestMapping(value = "/getdishlistY")
	public List<Dish> getDishList() {
		List<Dish> dish = cdishService.getDishList();
		return dish;
	}

	@ResponseBody
	@RequestMapping(value = "/adddishY")
	public int addDish(@RequestBody Dish dish) {
		int i = cdishService.addDish(dish);
		return i;
	}

	@ResponseBody
	@RequestMapping(value = "/updatedishY")
	public int updateDish(@RequestBody Dish dish) {
		int i = cdishService.updateDish(dish);
		return i;
	}

	@ResponseBody
	@RequestMapping(value = "/deldishY")
	public int delDish(@RequestParam("dish_id") String dish_id) {
		TodaymenuY todaymenu = ctodaymenuService.getTodaymenu(dish_id);
		TomorrowmenuY tomorrowmenu = ctomorrowmenuService.getTomorrowmenu(dish_id);
		if (tomorrowmenu == null && todaymenu == null) {
			cdishService.delDish(dish_id);
			return 1;
		} else {
			return 0;
		}
	}
}
